<template>
  <!-- 选择商品弹窗 -->
  <el-dialog
    title="选择商品"
    :visible.sync="selectProductDialog"
    width="800px"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-form ref="productForm" :model="productFilters" class="demo-form-inline">
      <el-form-item label="商品名称" prop="goodsName">
        <div class="product-form-item">
          <el-input
            class="input"
            v-model="productFilters.goodsName"
            placeholder="请输入"
            @keyup.enter.native="handleSearch"
          ></el-input>
        </div>
      </el-form-item>
      <el-form-item label="商品编码" prop="goodsCode">
        <div class="product-form-item">
          <el-input
            class="input"
            v-model="productFilters.goodsCode"
            placeholder="请输入"
            @keyup.enter.native="handleSearch"
          ></el-input>
          <div class="product-dialog-btns">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetForm('productForm')">重置</el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <el-table :data="productList">
      <!-- <el-table-column prop="id" label="ID"> </el-table-column> -->
      <el-table-column prop="goodsName" label="商品名称" width="120">
      </el-table-column>
      <el-table-column prop="categoryId" label="商品分类">
        <template slot-scope="scope">
          {{ selectCategoryIdLabel(scope.row.categoryId) }}
        </template>
      </el-table-column>
      <el-table-column label="商品头图">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.goodsPhoto"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsCode" label="商品编码"> </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="handleSelectRow(scope.row)"
            >选择</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { getCategoryListAPI, getGoodsListAPI } from "@/api/system/product";

export default {
  name: "SelectProductDialog",
  data() {
    return {
      selectProductDialog: false,
      productFilters: {
        goodsName: "",
        goodsCode: "",
      },
      total: 50,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
      },
      productList: [],
      options: [],
    };
  },
  computed: {
    selectCategoryIdLabel() {
      return (id) => {
        const item = this.options.find((item) => item.id == id);
        return item ? item.categoryName : "";
      };
    },
  },
  methods: {
    getCateList() {
      getCategoryListAPI({ pageNum: 1, pageSize: 999999 }).then((response) => {
        this.options = response.rows;
      });
    },
    openSelectProduct() {
      this.selectProductDialog = true;
      this.getList();
      this.getCateList();
    },
    getList() {
      getGoodsListAPI({ ...this.productFilters, ...this.queryParams }).then(
        (res) => {
          if (res.code == 200) {
            console.log(res, "返回数据");
            this.productList = res.rows;
            this.total = res.total;
          }
        }
      );
    },
    close() {
      this.selectProductDialog = false;
      this.resetForm("productForm");
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSearch() {
      console.log(this.productFilters, "开始查询");
      this.getList();
    },
    handleSelectRow(row) {
      this.$emit("handle-select", row);
    },
  },
};
</script>

<style lang="scss" scoped>
.product-form-item {
  display: flex;

  .input {
    width: 60%;
  }

  .product-dialog-btns {
    flex: 1;
    margin-left: 20px;
  }
}
</style>
